﻿var Tippelde = window.Tippelde || {};

var done = function (result, eventId) {
    var parentRow = $("#event-container-row-" + eventId);
    if (parentRow.next().next().hasClass("result-add-container"))
        parentRow.next().next().remove();
    if (parentRow.next().hasClass("bet-container"))
        parentRow.next().remove();
    parentRow.replaceWith(result);
};

Tippelde.SubmitEventForm = function (form) {
    if ($(form).valid()) {
        var eventId = $(form).find(":input[name=Id]").val();
        $.ajax({
            url: form.action,
            type: form.method,
            data: $(form).serialize()
        }).done(function (result) {
            done(result, eventId);
        });
    }
};

$(document).ready(function () {
    $(document).on('submit', 'form.event-form', function (e) {
        var form = this;
        if ($(form).hasClass("form-ajax-confirm")) {
            bootbox.confirm("Biztos törölni szeretnéd?", function (result) {
                if (result) {
                    Tippelde.SubmitEventForm(form);
                }
            });
        } else {
            Tippelde.SubmitEventForm(form);
        }
        e.preventDefault();
    });
});